-
Notifications
You must be signed in to change notification settings - Fork 2
/
wcsftime.c
227 lines (188 loc) · 7.79 KB
/
wcsftime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*++
toro C Library
https://github.com/KilianKegel/toro-C-Library#toro-c-library-formerly-known-as-torito-c-library
Copyright (c) 2017-2024, Kilian Kegel. All rights reserved.
SPDX-License-Identifier: GNU General Public License v3.0
Module Name:
wcsftime.c
Abstract:
Implementation of the Standard C function.
Format a time string.
Author:
Kilian Kegel
--*/
#include <CdeServices.h>
//#include <time.h>
struct tm
{
int tm_sec; // seconds after the minute - [0, 60] including leap second
int tm_min; // minutes after the hour - [0, 59]
int tm_hour; // hours since midnight - [0, 23]
int tm_mday; // day of the month - [1, 31]
int tm_mon; // months since January - [0, 11]
int tm_year; // years since 1900
int tm_wday; // days since Sunday - [0, 6]
int tm_yday; // days since January 1 - [0, 365]
int tm_isdst; // daylight savings time flag
};
extern int sprintf(char* pszDest, const char* pszFormat, ...);
extern time_t mktime(struct tm* ptm);
extern size_t strlen(const char* pszBuffer);
extern wchar_t* wmemcpy(wchar_t* s1, const wchar_t* s2, size_t n);
extern size_t mbstowcs(wchar_t* wcstr, const char* mbstr, size_t count);
extern void* __cdeGetAppIf(void);
extern char* wday_name_short[7];
extern char* mon_name_short[12];
extern char* wday_name_long[7];
extern char* mon_name_long[12];
extern char* ampm[2];
static int FirstYday(int year_since_1900) {
struct tm tm = { 0,0,0,1,0,0,0,0,0 };
tm.tm_year = year_since_1900;
mktime(&tm);
return tm.tm_wday;
}
static int Sun2MonBased(int wday) {
return (wday + 6) % 7;
}
/** wcsftime() -- string format time
Synopsis
#include <time.h>
size_t wcsftime(wchar_t *strDest, size_t maxsize, const wchar_t *format, const struct tm *timeptr);
Description
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/wcsftime-wcsftime-wcsftime-l-wcsftime-l
https://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf#page=397
The wcsftime function places characters into the array pointed to by s
as controlled by the string pointed to by format.
Returns
If the total number of resulting characters including the terminating null character is not
more than maxsize, the wcsftime function returns the number of characters placed
into the array pointed to by s not including the terminating null character.
Otherwise, zero is returned and the contents of the array are indeterminate.
@param[in] char *s
@param[in] size_t maxsize
@param[in] const char * format
@param[in] const struct tm * ptm
@retval size_t, number of bytes written, excluding termination zero
**/
size_t wcsftime(wchar_t* pDest, size_t maxsize, const wchar_t* format, const struct tm* ptm)
{
size_t nRet = 0;
int n, f, fSharp, fErr, fFinished, l;
char buffer[64];
wchar_t c;
enum STATE {
PROCESS_WRITECHARS,
PROCESS_PERCENT,
PROCESS_FORMAT,
PROCESS_ERROR,
PROCESS_COPYBUFFER,
PROCESS_SHARP
}state = PROCESS_WRITECHARS;
do {
// ----- check parameters
if (NULL == pDest ||
(int)ptm->tm_year < 70 ||
(int)ptm->tm_sec > 60 ||
(int)ptm->tm_min > 60 ||
(int)ptm->tm_hour > 23 ||
(int)ptm->tm_mday > 31 ||
(int)ptm->tm_mday < 1 ||
(int)ptm->tm_mon > 12 ||
(int)ptm->tm_wday > 6 ||
(int)ptm->tm_yday > 366) {
nRet = 0;
if (pDest)
pDest[0] = '\0';
break;
}
for (n = f = fErr = fFinished = 0, state = PROCESS_WRITECHARS; n < (int)maxsize && fErr == 0 && fFinished == 0; ) {
c = format[f];
switch (state) {
case PROCESS_WRITECHARS:
fSharp = 0;
if (c == '%') {
state = PROCESS_PERCENT;
}
else if (c == '#') {
state = PROCESS_SHARP;
}
else {
pDest[n++] = c;
fFinished = (c == '\0');
}
f++;
break;
case PROCESS_PERCENT:
if (c == '\0') {
state = PROCESS_ERROR;
break;
}
if (c == '%') {
pDest[n++] = c;
state = PROCESS_WRITECHARS;
f++;
break;
}
state = PROCESS_FORMAT;
break;
case PROCESS_SHARP:
fSharp = 1;
state = PROCESS_FORMAT;
break;
case PROCESS_ERROR:
if (0 != maxsize)
pDest[0] = '\0';
n = 0;
fErr = 1;
break;
case PROCESS_FORMAT:
switch (c) {
case 'a': f++; sprintf(buffer, "%s", wday_name_short[ptm->tm_wday]); break;
case 'A': f++; sprintf(buffer, "%s", wday_name_long[ptm->tm_wday]); break;
case 'b': f++; sprintf(buffer, "%s", mon_name_short[ptm->tm_mon]); break;
case 'B': f++; sprintf(buffer, "%s", mon_name_long[ptm->tm_mon]); break;
case 'c': f++; sprintf(buffer, "%s %s %2d %02d:%02d:%02d %04d", wday_name_short[ptm->tm_wday], mon_name_short[ptm->tm_mon], ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, 1900 + ptm->tm_year); break;
case 'd': f++; sprintf(buffer, "%02d", ptm->tm_mday); break;
case 'H': f++; sprintf(buffer, "%02d", ptm->tm_hour); break;
case 'I': f++; sprintf(buffer, "%02d", ptm->tm_hour % 12 == 0 ? 12 : ptm->tm_hour % 12); break;
case 'j': f++; sprintf(buffer, "%03d", 1 + ptm->tm_yday); break;
case 'm': f++; sprintf(buffer, "%02d", ptm->tm_mon + 1); break;
case 'M': f++; sprintf(buffer, "%02d", ptm->tm_min); break;
case 'p': f++; sprintf(buffer, "%s", ampm[ptm->tm_hour >= 12]); break;
case 'S': f++; sprintf(buffer, "%02d", ptm->tm_sec); break;
case 'U': f++; sprintf(buffer, "%02d", (0 == FirstYday(ptm->tm_year)) + ((FirstYday(ptm->tm_year) - ptm->tm_wday) > 0) + ptm->tm_yday / 7); break;
case 'w': f++; sprintf(buffer, "%d", ptm->tm_wday); break;
case 'W': f++; sprintf(buffer, "%02d", (1 == FirstYday(ptm->tm_year)) + (Sun2MonBased(FirstYday(ptm->tm_year)) - Sun2MonBased(ptm->tm_wday) > 0) + ptm->tm_yday / 7); break;
case 'x': f++; sprintf(buffer, "%02d/%02d/%02d", 1 + ptm->tm_mon, ptm->tm_mday, ptm->tm_year % 100); break;
case 'X': f++; sprintf(buffer, "%02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); break;
case 'y': f++; sprintf(buffer, "%02d", ptm->tm_year % 100); break;
case 'Y': f++; sprintf(buffer, "%d", 1900 + ptm->tm_year); break;
case 'Z': f++; sprintf(buffer, "Coordinated Universal Time"); break;
default: state = PROCESS_ERROR; break;
}
state = PROCESS_COPYBUFFER;
break;
case PROCESS_COPYBUFFER:
l = (int)strlen(buffer);
if (l + n < (int)maxsize) {
mbstowcs(&pDest[n], buffer, (size_t)l);
n += (int)l;
state = PROCESS_WRITECHARS;
}
else {
state = PROCESS_ERROR;
}
break;
}
}
if (n >= (int)maxsize && '\0' != pDest[n - 1])
n = 0;
if (n)
n--;
if (maxsize)
pDest[n] = '\0';
nRet = n;
} while (0);
return nRet;
}